<?php

function to_interest_array($interest_object) {
    $location 	= get_interest_location($interest_object->id);
    $photo 	= get_photo_url($interest_object->id);
    $photos     = get_photo_urls($interest_object->id);
    
    $photo_urls = array();
    foreach($photos as $p) {
        $photo_urls[] = $p->url;
    }
    
    return array(
        'guid'          => $interest_object->id,
        'id'            => $interest_object->id,
        'name'          => $interest_object->name,
        'description'	=> $interest_object->description,
        'category'      => get_category_name($interest_object->category_id),
        'image_url'     => $photo->url,
        'image_urls'    => $photo_urls,
        'location'      => array('lat' => $location->lat, 'lng' => $location->lng),
        'location_descriptive' => $location->descriptive_name,
        'dislike'       => $interest_object->dislikes,
        'like'          => $interest_object->likes,
        'neutral'		=> $interest_object->neutrals,
        'total_rating'  => (intval($interest_object->dislikes) + intval($interest_object->likes)),
        'source'        => intval($interest_object->source),
        'id_str'        => $interest_object->id_str,
        'time'          => $interest_object->timestamp
    );
}

function to_rating_array($rating_object) {
    $interest = get_interest($rating_object->object_id);
    $comment = count_interest_rating_comment($rating_object->history_id);
    return array(
        'guid'          => $rating_object->id,
        'history_id'	=> $rating_object->history_id,
        'user_guid'     => $rating_object->user_id,
        'interest'      => to_interest_array($interest),
        'rating'        => $rating_object->rating,
        'comment_count' => $comment,
        'time'          => $rating_object->timestamp
    );
}

function to_rating_history_array($rating_history_object) {
    $interest = get_interest($rating_history_object->object_id);
    $comment = count_interest_rating_comment($rating_history_object->id);
    return array(
        'guid'          => $rating_history_object->id,
        'history_id'	=> $rating_history_object->id,
        'user_guid'     => $rating_history_object->user_id,
        'interest'      => to_interest_array($interest),
        'rating'        => $rating_history_object->rating,
        'comment_count' => $comment,
        'time'          => $rating_history_object->timestamp
    );
}

function to_rating_array_with_user_object($rating_object) {
    $interest = get_interest($rating_object->object_id);
    $comment = count_interest_rating_comment($rating_object->history_id);
    $userObject = get_user($rating_object->user_id);
    return array(
        'guid'          => $rating_object->id,
        'history_id'	=> $rating_object->history_id,
        'user_guid'     => $rating_object->user_id,
        'user' 			=> userToArray($userObject, OPINIONS, FALSE),
        'interest'      => to_interest_array($interest),
        'rating'        => $rating_object->rating,
        'comment_count' => $comment,
        'time'          => $rating_object->timestamp
    );
}


function to_comment_array($comment_object) {
    
    $ownerEntity = get_entity($comment_object->user_id);
    return array(
        'guid' => $comment_object->id,
        'user' => userToArray($ownerEntity, OPINIONS, FALSE),
        'rating_id' => $comment_object->object_id,
        'comment' => $comment_object->comment,
        'time'  => $comment_object->timestamp
    );
    
}

function display_distance($distance) {
	
	$_distance_in_mile = $distance / 1.609344;
	$_distance = 0;
	if ($_distance_in_mile < 0.1) {
		$_distance = number_format(($_distance_in_mile * 5280), 1) . ' ft';
	} else {
		$_distance = number_format($_distance_in_mile, 1) . ' mile';
	}
	
	return $_distance;

}
?>